package com.store.app.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hyphenate.easeui.model.DBHelper_hy;
import com.hyphenate.easeui.model.recordBean_hy;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.BookedViewPagerAdapter;
import com.store.app.fragment.TheOrderBuyFragment;
import com.store.app.fragment.TheOrderFreeFragment;
import com.store.app.imlife.activity.ChatActivity;
import com.store.app.net.NetRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * 买单
 */
public class TheBuyOrderActivity extends FragmentActivity implements View.OnClickListener{
    public List<Fragment> fragments = new ArrayList<Fragment>();
    private TextView radioGroup_titlebar_2, radioGroup_titlebar_3;
    private TextView title;
    private ViewPager viewpager;
    private String showFragmentIndex = "0";
    private TextView tv2;
    private TextView tv3;
    private View tv_Right1;
    private NetRequest netRequest;
    private TextView tv_exception_order;
    private LinearLayout ll_exception_order;
    private String count_num;
    //数据库存储
    private DBHelper_hy db=null;
    private recordBean_hy rb=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_booked_buy);
        findView();
        setListener();
        initData();
        if(db==null){
            db=new DBHelper_hy(this);
            rb=new recordBean_hy();
        }
    }


    private void setListener() {
        findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        /**
         *
         */

        //跳到店东助手官方客服
        tv_Right1.setVisibility(View.VISIBLE);
        tv_Right1.setBackgroundResource(R.drawable.pic_guanfangkefu);
        tv_Right1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String chatid = "diandongzhushouserviceagent";
                String title  ="官方客服";
                Intent intent = new Intent(TheBuyOrderActivity.this, ChatActivity.class);
                intent.putExtra("userId", chatid);
                intent.putExtra("groupName", title);
                startActivity(intent);

                try {
                    rb.saveMember(db, "", chatid, title
                            , "","https://oss-img.meitianhui.com/communityDocs/kefu.jpg", "0");
                }catch (Exception e){
                }

            }
        });
    }

    private void findView() {
//		radioGroup = (RadioGroup) findViewById(R.id.booked_rg);
//		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//
//			@Override
//			public void onCheckedChanged(RadioGroup group, int checkedId) {
//				switch (checkedId) {
//					case R.id.booked_rb1:
//						viewpager.setCurrentItem(0);
//						break;
//					case R.id.booked_rb2:
//						viewpager.setCurrentItem(1);
//						break;
//					case R.id.booked_rb3:
//						viewpager.setCurrentItem(2);
//						break;
//					default:
//						break;
//				}
//
//			}
//		});

        viewpager = (ViewPager) findViewById(R.id.booked_viewpager);
        radioGroup_titlebar_2 = (TextView) findViewById(R.id.booked_rb2_titleck2);
        radioGroup_titlebar_3 = (TextView) findViewById(R.id.booked_rb3_titleck3);


        tv_Right1 = findViewById(R.id.tv_right1);
        tv_Right1.setVisibility(View.VISIBLE);
        tv_Right1.setBackgroundResource(R.drawable.backfankui);

        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        title = (TextView) findViewById(R.id.tvTitle);
        title.setText("买单");
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
    }

    private void refreshUITab(String index) {
        if (index.equals("0")) {
            tv2.setTextColor(Color.parseColor("#f96116"));
            tv3.setTextColor(Color.parseColor("#333333"));
            radioGroup_titlebar_2
                    .setBackgroundColor(getResources()
                            .getColor(R.color.booked_color));
            radioGroup_titlebar_3
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
        } else if (index.equals("1")) {

            tv3.setTextColor(Color.parseColor("#f96116"));
            tv2.setTextColor(Color.parseColor("#333333"));
            radioGroup_titlebar_2
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
            radioGroup_titlebar_3
                    .setBackgroundColor(getResources()
                            .getColor(R.color.booked_color));
        }
    }

    private void initData() {
        fragments.add(new TheOrderBuyFragment());
        fragments.add(new TheOrderFreeFragment());
        BookedViewPagerAdapter disfratab = new BookedViewPagerAdapter(getSupportFragmentManager(), viewpager, fragments);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refreshUITab(position + "");
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        refreshUITab(showFragmentIndex);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv2:
                showFragmentIndex = "0";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(0);
                break;
            case R.id.tv3:
                showFragmentIndex = "1";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(1);
                break;
        }
    }



}